import 'package:get/get.dart';
import 'package:laos_mall/app_route.dart';
import 'package:laos_mall/common/base/page_size_controller.dart';
import 'package:laos_mall/common/utils/http_util.dart';
import 'package:laos_mall/common/widght/add_photo_view.dart';
import 'package:laos_mall/common/widght/toast/showtoast.dart';
import 'package:laos_mall/models/category/para_product.dart';
import 'package:laos_mall/models/category/product_list_model.dart';
import 'package:laos_mall/pages/main/shopping_cart/logic.dart';
import 'package:laos_mall/user_data.dart';

import '../catergory/sku/view.dart';

class ProductItemLogic extends PageSizeLogic<ProductItemModel> {
   final ParamProduct? param ;
   final String? tag;
  ProductItemLogic({this.param,this.tag}):super();
  @override
  void initConfig() {
    if(tag == "cart"){
      autoPage = false;
    }
    super.initConfig();
  }
  @override
  Future<List<ProductItemModel>?> Function()? get future => ()async{
    if(tag == "cart"){
      return guessLike();
    }
    var paramData = param ?? ParamProduct();
    paramData.page = params.pageNum;
    var arg = paramData.toJson();
    var para = ArithmeticUtil().removeNull(arg);
    var res = await HttpUtil.restApi.productList(para);
    return res.data?.product_list;
  };

   Future<List<ProductItemModel>?>guessLike()async{

     var paramData = param ?? ParamProduct();
     paramData.page = params.pageNum;
     var arg = paramData.toJson();
     var para = ArithmeticUtil().removeNull(arg);
     var res = await HttpUtil.restApi.guessLike(para);
     return res.data?.product_list;
   }


   collectProduct(int index,ProductItemModel item)async{
     var param = {"product_id":item.product_id};
     if(item.is_collect == false || item.is_collect == null){
       await HttpUtil.restApi.collectSave(param);
     }else if(item.is_collect == true){
       await HttpUtil.restApi.collectCancel(param);
     }
     dataSource[index].is_collect = !(dataSource[index].is_collect ?? false);
     update();
   }


   addCart(ProductItemModel product) {
     UserData().loginJudge(callback: ()async {
       if(product.product_sku.firstOrNull != null) {
         // Get.bottomSheet(SkuPage(skuId: product.,productId: product.product_id,));
         Get.toNamed(AppRouter.productDetail,arguments: {"id":product.product_id,"sku_id":product.product_sku.firstOrNull?.sku_id });
       }else{
         ///todo:  is_quick 首页未提供
         var param = {"id":product.product_id,"number":1,"sku_id":0,"is_quick":0};
         var res = await HttpUtil.restApi.addToCart(param);
         if(res.code == 0){
           showToast("hint_add_cart_success".tr,type: ToastType.success);
           UserData().cartNum += 1;
           final logic = Get.find<ShoppingCartLogic>();
           logic.onRefresh();
         }else{
           showToast("hint_add_cart_fail".tr,type: ToastType.error);
         }
       }
     });
   }
}
